package mysql

import "gorm.io/gorm"

type Order struct {
	gorm.Model
	UserId     int64   `gorm:"type:int(10)" json:"user_id"`
	DockerId   int64   `gorm:"type:int(10)" json:"docker_id"`
	HospitalId int64   `gorm:"type:int(10)" json:"hospital_id"`
	Price      float64 `gorm:"decimal(10,2)" json:"price"`
	LookTime   string  `gorm:"datetime" json:"look_time"`
	OrderSn    string  `gorm:"type:char(36)" json:"order_sn"`
	Status     int64   `gorm:"type:int(3);default:1" json:"status"`
	Num        int64   `gorm:"type:int(3);default:1" json:"num"`
}

func (o *Order) CreateOrder(userId, dockerId, hospitalId int64, price float64, lookTime, orderSn string) error {
	order := &Order{
		UserId:     userId,
		DockerId:   dockerId,
		HospitalId: hospitalId,
		Price:      price,
		LookTime:   lookTime,
		OrderSn:    orderSn,
	}
	err := DB.Create(&order).Error
	if err != nil {
		return err
	}
	return nil
}
func (o *Order) FindOrder(dockerId int64, lookTime string) (order []*Order, err error) {
	err = DB.Where("docker_id=? AND look_time=?", dockerId, lookTime).Find(&order).Error
	if err != nil {
		return nil, err
	}
	return order, nil
}
func (o *Order) FindOrders(userId int64) (order []*Order, err error) {
	err = DB.Where("user_id=?", userId).Find(&order).Error
	if err != nil {
		return nil, err
	}
	return order, nil
}
func (o *Order) FindUserByOr(userId int64) (*Order, error) {
	err := DB.Where("user_id=? And status=1", userId).Find(&o).Error
	if err != nil {
		return nil, err
	}
	return o, nil
}
